<template>
  <div class="app-container">
    <el-dialog :title="titles[flag]" :visible.sync="dialogVisible" width="60%">
      <el-form ref="form" :model="form" :disabled="flag==1" v-loading="loading">
        <div class="myTitle">基本信息</div>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="套餐名称" prop="saleBomId" required>
              <div v-if="flag==1">{{ form.bomName }}</div>
              <el-select v-else v-model="form.saleBomId" filterable style="width: 100%"
                         :disabled="flag==2" @change="changeBom">
                <el-option
                  v-for="item in bomList"
                  :key="item.id"
                  :label="item.bomName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="主商品名称" prop="productName" required>
              <div v-if="flag==1">{{ form.productName }}</div>
              <el-input v-else v-model="form.productName" placeholder="自动生成" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="定价方式" prop="priceType" required>
              <dict-tag v-if="flag==1" :options="dict.type.ats_price_type" :value="form.priceType"/>
              <el-select v-else v-model="form.priceType" placeholder="自动生成" style="width: 100%" disabled>
                <el-option
                  v-for="item in dict.type.ats_price_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="适用客户" prop="customerLevelIds" required>
              <div v-if="flag==1">{{ form.customerLevelDescs.toString() }}</div>
              <el-select v-else v-model="form.customerLevelIds" multiple filterable style="width: 100%">
                <el-option
                  v-for="item in dict.type.sys_merchant_level"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.priceType==2">
            <el-form-item label="套餐价格(元)" prop="salePrice" required>
              <div v-if="flag==1">{{ form.salePrice }}</div>
              <el-input v-else type="number" v-model="form.salePrice" :min="0" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.priceType==2">
            <el-form-item label="税率(%)" prop="taxRate" required>
              <div v-if="flag==1">{{ form.taxRate }}</div>
              <el-input v-else type="number" v-model="form.taxRate" :min="0" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" v-if="form.priceType==2">
          <el-col :span="8">
            <el-form-item label="税额(元)" prop="taxPrice">
              <div v-if="flag==1">{{ form.taxPrice }}</div>
              <el-input v-else v-model="form.taxPrice" placeholder="自动生成" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="含税价格(元)" prop="taxSalePrice">
              <div v-if="flag==1">{{ form.taxSalePrice }}</div>
              <el-input v-else v-model="form.taxSalePrice" placeholder="自动生成" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="myTitle">规格信息</div>
        <el-table
          :data="skuList"
          max-height="300"
        >
          <el-table-column type="index" label="序号" align="center"></el-table-column>
          <el-table-column prop="productName" label="商品名称" align="center"/>
          <el-table-column prop="skuName" label="规格名称" align="center"/>
          <el-table-column prop="quantity" label="数量" align="center"/>
          <el-table-column prop="salePrice" label="售卖价格(元)" align="center">
            <template slot-scope="scope">
              <div v-if="flag==1">{{ scope.row.salePrice }}</div>
              <el-input v-else type="number" v-model="scope.row.salePrice" :min="0" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="taxRate" label="税率(%)" align="center">
            <template slot-scope="scope">
              <div v-if="flag==1">{{ scope.row.taxRate }}</div>
              <el-input v-else type="number" v-model="scope.row.taxRate" :min="0" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="taxPrice" label="税额(元)" align="center"/>
          <el-table-column prop="taxSalePrice" label="含税价格(元)" align="center"/>
        </el-table>
        <div class="myTitle">配件信息</div>
        <el-table
          :data="matList"
          max-height="300"
        >
          <el-table-column type="index" label="序号" align="center"></el-table-column>
          <el-table-column prop="productName" label="配件名称" align="center"/>
          <el-table-column prop="skuName" label="规格名称" align="center"/>
          <el-table-column prop="quantity" label="数量" align="center"/>
          <el-table-column prop="salePrice" label="售卖价格(元)" align="center">
            <template slot-scope="scope">
              <div v-if="flag==1">{{ scope.row.salePrice }}</div>
              <el-input v-else type="number" v-model="scope.row.salePrice" :min="0" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="taxRate" label="税率(%)" align="center">
            <template slot-scope="scope">
              <div v-if="flag==1">{{ scope.row.taxRate }}</div>
              <el-input v-else type="number" v-model="scope.row.taxRate" :min="0" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="taxPrice" label="税额(元)" align="center"/>
          <el-table-column prop="taxSalePrice" label="含税价格(元)" align="center"/>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import {addSalePrice, updateSalePrice, listSaleBomSelect, getSaleBom} from "../../../../api/saleMng";

export default {
  dicts: ['sys_merchant_level', 'ats_price_type'],
  data() {
    return {
      flag: 0,
      titles: ['新增', '详情', '修改'],
      loading: false,
      dialogVisible: false,
      form: {saleBomId: ''},
      bomList: [],
      skuList: [],
      matList: []
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.skuList = []
      this.matList = []
      this.resetForm('form')
      this.form = form
      this.load()
      if (flag) {
        this.skuList = this.form.skuItemList
        this.matList = this.form.partItemList
      }
    },
    load() {
      if (this.flag != 1) {
        listSaleBomSelect().then(res => {
          this.bomList = res.data
        }).finally(e => {
          this.loading = false
        })
      }
    },
    changeBom() {
      getSaleBom(this.form.saleBomId).then(res => {
        let data = res.data
        this.form.productName = data.productName
        this.form.priceType = data.priceType
        this.skuList = data.skuList
        this.matList = data.partList
      })
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.form.skuItemList = this.skuList
          this.form.partItemList = this.matList
          this.loading = true
          if (!this.flag) {
            addSalePrice(this.form).then(res => {
              this.dialogVisible = false
              this.$emit('ok')
              this.$message.success('新增成功')
            }).finally(e => {
              this.loading = false
            })
          } else {
            updateSalePrice(this.form).then(res => {
              this.dialogVisible = false
              this.$emit('ok')
              this.$message.success('修改成功')
            }).finally(e => {
              this.loading = false
            })
          }
        }
      })
    },
  }
}
</script>
